package exttable.userdefineextendsclasses.userDefineJFileChooser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.zip.CRC32;

public class UserDefineJFileChooserWithView extends JPanel {
    private JButton openFileJButton;
    private JLabel checkJLabel;
    private JButton crossJButton;
    private ImageIcon checkIcon;
    private ImageIcon crossIcon;
    private JFileChooser jFileChooser;
    private int retVal;
    private File selectedFile;
    private JLabel sizeLabel;
    private double selectedFileSize;


    public File getSelectedFile() {
        return selectedFile;
    }

    public void setSelectedFile(File selectedFile) {
        this.selectedFile = selectedFile;
    }

    public UserDefineJFileChooserWithView(String checkIconPath, String crossIconPath, File file) {
        this.selectedFile = file;
        jFileChooser = new JFileChooser();
        openFileJButton = new JButton();
        checkIcon = new ImageIcon(UserDefineJFileChooserWithView.class.getResource(checkIconPath));
        crossIcon = new ImageIcon(UserDefineJFileChooserWithView.class.getResource(crossIconPath));

        checkJLabel = new JLabel(checkIcon);

        crossJButton = new JButton(crossIcon);
        crossJButton.addActionListener(unSelectFile());
        crossJButton.setPreferredSize(new Dimension(crossIcon.getIconWidth(), crossIcon.getIconHeight()));

        sizeLabel = new JLabel();

        if (selectedFile == null) {
            System.out.println("file is null");
            organizeSelectGui();
        } else {
            System.out.println("file is not null");
            organizeViewGui();
        }

        add(openFileJButton);
        add(sizeLabel);
        add(checkJLabel);
        add(crossJButton);
//		JPanel panelGrid = new JPanel(new GridLayout(1, 3, 1, 0));
//		panelGrid.add(openFileJButton);
//		panelGrid.add(checkJLabel);
//		panelGrid.add(crossJButton);
//		add(panelGrid);
    }

    private ActionListener selectFile() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                retVal = jFileChooser.showOpenDialog(openFileJButton);
                if (retVal == JFileChooser.APPROVE_OPTION) {
                    selectedFile = jFileChooser.getSelectedFile();
                    organizeViewGui();
                }

            }
        };
    }

    private ActionListener unSelectFile() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                selectedFile = null;
                System.out.println("selectedFile: " + selectedFile);
                organizeSelectGui();


            }
        };
    }

    public static String calculateSumCRC32(File file) {
        /**
         * This method return sum CRC32 of file
         */
        CRC32 crc32 = new CRC32();
        byte[] buf = new byte[8000];
        int nLength;
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            while ((nLength = fileInputStream.read(buf)) >= 0) {
                crc32.update(buf, 0, nLength);
            }
            fileInputStream.close();
            return Long.toString(crc32.getValue());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    private void organizeSelectGui() {
        openFileJButton.setText("select file");
        openFileJButton.setToolTipText("select file");

        if (openFileJButton.getActionListeners().length > 0) {
            openFileJButton.removeActionListener(openFileJButton.getActionListeners()[0]);
        }
        openFileJButton.addActionListener(selectFile());

        checkJLabel.setVisible(false);
        crossJButton.setVisible(false);
        sizeLabel.setText("");

    }

    private void organizeViewGui() {
        openFileJButton.setText(selectedFile.getName());
        openFileJButton.setToolTipText("view file");

        if (openFileJButton.getActionListeners().length > 0) {
            openFileJButton.removeActionListener(openFileJButton.getActionListeners()[0]);
        }

        openFileJButton.addActionListener(viewFile());
        checkJLabel.setVisible(true);
        crossJButton.setVisible(true);
        selectedFileSize = new BigDecimal((double) selectedFile.length() / 1024).setScale(1, RoundingMode.UP).doubleValue();
        sizeLabel.setText(selectedFileSize + "kb");

    }

    private ActionListener viewFile() {
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.open(selectedFile);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        };

    }


}
